<?php

namespace App\Http\Controllers\Admin\Logs;

use App\Http\Controllers\AdminController;
use App\Models\Logs\LoginLog;
use Illuminate\Http\Request;

class LoginLogController extends AdminController
{

    public function data(Request $request)
    {
        $model = LoginLog::query()->select('id', 'username', 'message', 'platform', 'browser', 'ip', 'ip_address', 'created_at');
        if ($ip = $request->get('ip')) {
            $model->where('ip', 'like', $ip.'%');
        }
        if ($username = $request->get('username')) {
            $model->where('username', 'like', $username.'%');
        }
        $total = $model->count();
        $page = (int)$request->get('page', 1);
        $pageSize = (int)$request->get('pageSize', 10);
        $list = $model->orderByDesc('id')->offset(($page-1)*$pageSize)->limit($pageSize)->get();
        return $this->succeed([
            'total' => $total,
            'rows' => $list,
        ]);
    }
}
